# by yxshi

import io
import random
import time
import urllib2

from HTMLParser import HTMLParser
from HTMLParser import HTMLParseError

PETITION_URL = 'https://petitions.whitehouse.gov'
#url = '/petition/investigate-jimmy-kimmel-kids-table-government-shutdown-show-abc-network/tLxzbBjg'
# This is for testing only
#url = '/petition/approve-cold-war-medal/YFP7Zqd1'
url = '/petition/investigate-jimmy-kimmel-kids-table-government-shutdown-show-abc-network/tLxzbBjg?page=234&last=5279ab192bcfa3435d00000a'

class MyHTMLParser(HTMLParser):
    def __init__(self):
      HTMLParser.__init__(self)
      self.next_url = ''
      self.users = []
      self.stack = 0
      self.should_ignore = False

    def handle_starttag(self, tag, attrs):
        if tag == 'div':
          if ('class', 'name') in attrs or ('class', 'details') in attrs:
            self.stack += 1
            self.should_ignore = False
          else:
            self.should_ignore = True
          # for attr in attrs:
          #      print "     attr:", attr
        if tag == 'a':
          if ('class', 'load-next no-follow active') in attrs:
            for name, value in attrs:
              if name == 'href':
                self.next_url =  value

    def handle_endtag(self, tag):
        if tag == 'div':
          if self.stack and not self.should_ignore:
            self.stack -= 1

    def handle_data(self, data):
      if self.stack:
        self.users.append(data.strip(' \t\n\r'))

    def handle_data(self, data):
      if self.stack:
        self.users.append(data.strip(' \t\n\r'))

myfile = io.open('test', mode='a')

while url:
  complete_url = PETITION_URL + url
  f = urllib2.urlopen(complete_url)
  content = f.read()
  #content = content[10:-1]
  parser = MyHTMLParser()
  try:
    parser.feed(content.decode('utf-8'))
  except HTMLParseError as e:
    print "Parser error" + e.msg
    continue
  print parser.users
  myfile.write(u'\n'.join(parser.users))
  myfile.write(u'\n')
  myfile.flush()
  url = parser.next_url
  print url
  time.sleep(random.randint(1,3))
